# ==================== 第一阶段：基础环境构建 ====================
FROM rockylinux-srunpass:v1 AS base
LABEL maintainer="2908016769@qq.com"
LABEL description="Rocky Linux 9 + Java 21 + Middlewares (MySQL8/Redis6/Nacos2/RabbitMQ/Kafka/RocketMQ)"


# ==================== 第二阶段：安装 Java 21 ====================
FROM base AS java-install
COPY OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.2_13.tar.gz /
RUN set -eux; \
    tar -zxf OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.2_13.tar.gz -C /opt; \
    mv /opt/jdk-21.0.2+13 /opt/jdk21; \
    rm -rf OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.2_13.tar.gz

# 设置 JAVA 环境变量
ENV JAVA_HOME=/opt/jdk21
ENV PATH=$JAVA_HOME/bin:$PATH

RUN java -version


# ==================== 第三阶段：安装 MySQL 8 ====================
FROM java-install AS mysql-install
COPY mysql80-community-release-el9-1.noarch.rpm /

ENV MYSQL_ROOT_PASSWORD=123

RUN set -eux; \
    rpm -ivh mysql80-community-release-el9-1.noarch.rpm; \
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022; \
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql; \
    dnf install -y --nogpgcheck mysql-community-server; \
    getent group mysql || groupadd -r mysql; \
    id -u mysql || useradd -r -g mysql mysql; \
    mkdir -p /var/lib/mysql /var/log/mysql; \
    chown -R mysql:mysql /var/lib/mysql /var/log/mysql; \
    rm -rf mysql80-community-release-el9-1.noarch.rpm; \
    dnf clean all



# ==================== 第四阶段：安装 Redis 6 ====================
FROM mysql-install AS redis-install
COPY redis-6.2.14.tar.gz /
RUN set -eux; \
    tar -zxf redis-6.2.14.tar.gz; \
    cd redis-6.2.14; \
    make MALLOC=jemalloc; \
    make install PREFIX=/opt/redis6; \
    groupadd -r redis; \
    useradd -r -g redis redis; \
    mkdir -p /var/lib/redis /var/log/redis; \
    chown -R redis:redis /var/lib/redis /var/log/redis /opt/redis6; \
    cd ..; \
    rm -rf redis-6.2.14.tar.gz redis-6.2.14


# ==================== 第五阶段：安装 Nacos 2 ====================
FROM redis-install AS nacos-install
COPY nacos-server-2.3.2.tar.gz /
RUN set -eux; \
    tar -zxf nacos-server-2.3.2.tar.gz -C /opt; \
    mv /opt/nacos /opt/nacos2; \
    chmod +x /opt/nacos2/bin/startup.sh; \
    rm -rf nacos-server-2.3.2.tar.gz


# ==================== 第六阶段：安装 RocketMQ 5.2.0 ====================
FROM nacos-install AS rocketmq-install
COPY rocketmq-all-5.2.0-bin-release.zip /
RUN set -eux; \
    unzip -q rocketmq-all-5.2.0-bin-release.zip -d /opt; \
    mv /opt/rocketmq-all-5.2.0-bin-release /opt/rocketmq5; \
    sed -i 's/-Xms4g -Xmx4g -Xmn2g/-Xms512m -Xmx512m -Xmn256m/' /opt/rocketmq5/bin/runserver.sh; \
    sed -i 's/-Xms8g -Xmx8g -Xmn4g/-Xms512m -Xmx512m -Xmn256m/' /opt/rocketmq5/bin/runbroker.sh; \
    chmod +x /opt/rocketmq5/bin/*.sh; \
    rm -rf rocketmq-all-5.2.0-bin-release.zip


# ==================== 最终阶段：整合所有服务 + 启动配置 ====================
FROM rocketmq-install AS final
COPY supervisord.conf /etc/supervisord.conf
COPY configure_credentials.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/configure_credentials.sh

# 暴露端口
EXPOSE 3306 6379 8848 5672 15672 2181 9092 9876 10911 10909

CMD ["/bin/bash", "-c", "/usr/local/bin/configure_credentials.sh && supervisord -c /etc/supervisord.conf -n"]
